package main

import (
	"fmt"
	"time"

	"github.com/opentracing/opentracing-go"

	"github.com/uber/jaeger-client-go"
	jaegercfg "github.com/uber/jaeger-client-go/config"
)

func main() {
	cfg := jaegercfg.Configuration{
		Sampler: &jaegercfg.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &jaegercfg.ReporterConfig{
			LogSpans:           true,
			LocalAgentHostPort: "192.168.4.170:6831",
			//CollectorEndpoint: "http://120.24.67.46:14268/api/traces",
		},
		ServiceName: "mxshop",
	}

	tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
	if err != nil {
		fmt.Println("出错了")
		panic(err)
	}
	defer closer.Close()

	// 发送一个span
	span := tracer.StartSpan("go-grpc-web")
	time.Sleep(time.Second * 1)
	span.Finish()

	parenSpan := tracer.StartSpan("main")
	// 发送一个嵌套span
	span1 := tracer.StartSpan("functionA", opentracing.ChildOf(parenSpan.Context()))
	time.Sleep(time.Second * 1)
	span1.Finish()
	// 发送一个嵌套span
	span2 := tracer.StartSpan("functionB", opentracing.ChildOf(parenSpan.Context()))
	time.Sleep(time.Second * 2)
	span2.Finish()
	parenSpan.Finish()
	// Reporting span 229e0fffe15ed530:7d054e5257a86906:229e0fffe15ed530:1
	// 229e0fffe15ed530是trance的id
	// 7d054e5257a86906是span的id
	// 最后一个229e0fffe15ed530是父级trance的id
}
